from multiprocessing.dummy import Pool

import requests
from lxml import etree

# referer
# https://blog.csdn.net/python_neophyte/article/details/82562330
# https://www.cnblogs.com/cuihongyu3503319/p/15248239.html

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
}

responseText = requests.get("https://www.pearvideo.com/category_5", headers=headers).text
tree = etree.HTML(responseText)
a_list = tree.xpath('//*[@id="listvideoListUl"]/li/div/a')
dicts = []

for a in a_list:
    href = a.xpath('./@href')[0]

    url = 'https://www.pearvideo.com/' + href
    name = a.xpath('./div[2]/text()')[0] + '.mp4'
    # requests.get(url=url, headers=headers).text

    contId = href.split("_")[-1]
    params = {
        "contId": contId,
        "mrd": 0.44547956490149754
    }
    # header中配置Referer 防盗链
    json = requests.get("https://www.pearvideo.com/videoStatus.jsp", params=params, headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62",
        "Referer": url
    }).json()
    # print(json)
    dicts.append({
        "name": name,
        "url": json['videoInfo']['videos']['srcUrl'],
        "contId": contId,
    })


# 学习别人如何反爬虫
def down_video(dic):
    # old: 'https://video.pearvideo.com/mp4/short/20220130/1644639834451-15822478-hd.mp4'
    # to
    # replace: 'https://video.pearvideo.com/mp4/short/20220130/cont-1747969-15822478-hd.mp4'
    replace_str = dic['url'].split("/")[-1]
    tail_replace_str = replace_str.replace(replace_str.split("-")[0], 'cont-' + dic['contId'])
    video_url = dic['url'].replace(replace_str, tail_replace_str)
    # print(video_url)
    print(dic['name'], '正在下载......')
    with open(dic['name'], 'wb') as fp:
        fp.write(requests.get(video_url, headers=headers).content)
        print(dic['name'], '下载成功！')


pool = Pool(4)
pool.map(down_video, dicts)
pool.close()
pool.join()
